<?php
namespace AuctionCore\CarDealer;

use AuctionCore\Account\Back\Department;
use AuctionCore\AntNest\UserCenter;
use AuctionCore\CarDealer\Dealer;
use AuctionCore\Common\Request;
use AuctionCore\System\ListBase;

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//


/**
 * "车商帐号"列表
 *
 * @author       爱民
 */
class DealerUserList
{

    public function __construct()
    {

    }

    public function getList($page, $limit, $params)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $limit = (int)$limit < 1 ? 10 : (int)$limit;
        //搜索
        $map = array();
        //客户经理
        if ($params['manager_id'] > 0) {
            $map['dm.emp_id'] = intval($params['manager_id']);
        }
        if (!$params['auth']) {
            $map['u.status'] = ['eq', 0];  //启用
        }
        //账号
        if (!empty($params['user_name'])) {
            $map['u.user_name'] = array('eq', $params['user_name']);
        }
        //联系人
        if (!empty($params['contact_person'])) {
            $map['cd.contact_person'] = array('like', '%' . $params['contact_person'] . '%');
        }
        //联系人电话
        if (!empty($params['contact_mobile'])) {
            $map['cd.contact_mobile'] = array('eq', $params['contact_mobile']);
        }
        //分公司ID
        if ($params['bc_id'] >= 0) {
            $map['cd.depart_id'] = array('eq', $params['bc_id']);
        }
        if ($params['branch_id']) {
            $map['cd.depart_id'] = array('eq', $params['branch_id']);
        }
        //账户类型
        if ($params['category'] > 0) {
            $map['cd.category'] = array('eq', $params['category']);
        }
        //账户状态
        if ($params['status'] === 0 || $params['status'] === 1) {
            $map['u.status'] = ['eq', $params['status']];
        }
        //要查询的列
        $cols = "cd.dealer_id,cd.category,cd.contact_person,cd.contact_mobile,cd.city,cd.dealer_type,
                cd.depart_id,dm.emp_id,u.uid,u.user_name,u.status,u.reg_date,e.real_name";

        $prefix = C("DB_PREFIX");
        $total = M('car_dealer')->alias('cd')
            ->join("{$prefix}dealer_user as du ON du.dealer_id=cd.dealer_id")
            ->join("{$prefix}user as u ON du.uid=u.uid")
            ->join("{$prefix}dealer_manager as dm ON cd.dealer_id=dm.dealer_id")
            ->join("left join {$prefix}employee e ON dm.emp_id=e.emp_id")
            ->where($map)->count();

        $list = M('car_dealer')->alias('cd')->field($cols)
            ->join("{$prefix}dealer_user as du ON du.dealer_id=cd.dealer_id")
            ->join("{$prefix}user as u ON du.uid=u.uid")
            ->join("{$prefix}dealer_manager as dm ON cd.dealer_id=dm.dealer_id")
            ->join("left join {$prefix}employee e ON dm.emp_id=e.emp_id")
            ->where($map)->order("u.uid desc")->limit($limit)->page($page)->order("cd.dealer_id desc")->select();
        $addr = new \AuctionCore\Other\Address();
        foreach ($list as $k => $v) {
            $depart_info = Department::getDepartById($v['depart_id']);
            $branch_company_info = Department::getDepartById($depart_info['branch_id']);
            $list[$k]['branch_company'] = isset($branch_company_info['name']) ? $branch_company_info['name'] : '总部';
            $location_area = $addr->get_name_by_code($v['city']);
            $list[$k]['location_area'] = $location_area;
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;

    }

    public function dealerList($page, $limit, $params)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $limit = (int)$limit < 1 ? 10 : (int)$limit;
        //搜索
        $map = array();
        if ($params['dealer_id'] > 0) {
            $map['cd.dealer_id'] = ['eq', intval($params['dealer_id'])];
        }
        if (!empty($params['user_name'])) {
            // 获取uid,查找dealer_id,查找车商信息
            $uc = new UserCenter();
            $result = $uc->infoByAccountName($params['user_name'], 2);
            if (!empty($result))
                $map['du.uid'] = $result['account_id'];
        }
        if ($params['contact_person']) {
            $map['cd.contact_person'] = ['LIKE', '%' . $params['contact_person'] . '%'];
        }
        if ($params['contact_mobile']) {
            $uc = new UserCenter();
            $info1 = $uc->info_bymobile($params['contact_mobile'], 2, 1);
            $map['du.uid'] = array('eq', $info1['account_id']);
            //$map['cd.contact_mobile'] = ['LIKE', '%' . $params['contact_mobile'] . '%'];
        }
        if ($params['business_city']) {
            $map['cd.business_city'] = ['eq', $params['business_city']];
        }
        if ($params['bc_id'] > -1) {
            $map['cd.depart_id'] = ['eq', $params['bc_id']];
        }
        if ($params['auth_type'] > -1) {
            if ($params['auth_type'] > 0) {
                $map['cd.auth_type'] = ['eq', $params['auth_type']];
                $map['cd.auth_status'] = ['eq', 2];
            } else {
                $map['cd.auth_status'] = array('neq', 2);
            }
        }
        if ($params['level'] > -1) {
            $map['cd.level'] = ['eq', $params['level']];
        }
        //客户经理
        if ($params['manager_id'] > 0) {
            $map['dm.emp_id'] = ['eq', intval($params['manager_id'])];
        }
        if ($params['manager_id'] == '-1') {
            $where['dm.emp_id'] = ['lt', 1];
            $map['dm.emp_id'] = ['exp', 'is NULL'];
            $map['_complex'] = $where;
            $map['_logic'] = "OR";
        }
//        if(!$params['auth']){
//            $map['u.status'] = ['eq',0];  //启用
//        }
//        //账号
//        if(!empty($params['user_name'])){
//            $map['u.user_name'] = array('eq',$params['user_name']);
//        }
        //创建时间
        if ($params['start_time']) {
            $map['cd.createtime'] = ['egt', $params['start_time'] . ' 00:00:00'];
        }
        if ($params['end_time']) {
            $map['cd.createtime'] = ['egt', $params['end_time'] . ' 23:59:59'];
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['cd.createtime'] = [
                ['egt', $params['start_time'] . ' 00:00:00'],
                ['elt', $params['end_time'] . ' 23:59:59']
            ];
        }
        //账户状态
        if ($params['status']) {
            $map['cd.status'] = ['eq', $params['status']];
        }
        //要查询的列
        $cols = "cd.dealer_id,cd.category,cd.contact_person,du.uid,cd.city,cd.business_city,
                 cd.auth_type,cd.auth_status,cd.dealer_type,cd.level,cd.depart_id,dm.emp_id,e.real_name,du.uid";
        //u.uid,u.user_name,u.status,u.reg_date,
        $prefix = C("DB_PREFIX");
        $total = M('car_dealer')->alias('cd')
            ->join("{$prefix}dealer_user as du ON du.dealer_id=cd.dealer_id")
            ->join("LEFT JOIN {$prefix}dealer_manager as dm ON cd.dealer_id=dm.dealer_id")
            ->join("left join {$prefix}employee e ON dm.emp_id=e.emp_id")
            ->where($map)->count();
        $list = M('car_dealer')->alias('cd')->field($cols)
            ->join("{$prefix}dealer_user as du ON du.dealer_id=cd.dealer_id")
            ->join("LEFT JOIN {$prefix}dealer_manager as dm ON cd.dealer_id=dm.dealer_id")
            ->join("LEFT JOIN {$prefix}employee e ON dm.emp_id=e.emp_id")
            ->where($map)->limit($limit)->page($page)->order("cd.dealer_id desc")->select();
        if (!empty($list)) {
            $dealer = new Dealer();
            $list = $dealer->get_contact_mobile_byAntNest($list);
        }
        $addr = new \AuctionCore\Other\Address();
        $uidArr = [];
        foreach ($list as $k => $v) {
            $depart_info = Department::getDepartById($v['depart_id']);
            $branch_company_info = Department::getDepartById($depart_info['branch_id']);
            $list[$k]['branch_company'] = $branch_company_info['name'];
            $location_area = $addr->get_name_by_code($v['city']);
            $list[$k]['location_area'] = $location_area;
            if ($v['business_city'] > 0) {
                if ($v['business_city'] == 200) {
                    $list[$k]['business_city_name'] = '其他';
                } else {
                    $list[$k]['business_city_name'] = $addr->get_name_by_code($v['business_city']);
                }
            } else {
                $list[$k]['business_city_name'] = '未指定';
            }
            $list[$k]['level_name'] = C('DEALER_LEVEL')[$v['level']];
            $uidArr[] = $v['uid'];
        }
        $userList = array();
        if (!empty($uidArr)) {
            $uc = new UserCenter();
            $userList = $uc->batchInfo($uidArr, 2);
        }
        $result = array(
            'list' => $list,
            'user_list' => $userList['list'],
            'total' => $total
        );
        return $result;
    }
}
